/**
 *
 */
package com.nicefact.mailmonitor.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.stereotype.Component;

import com.nicefact.mailmonitor.vo.GridRequest;
import com.nicefact.mailmonitor.vo.PaginationInfo;
import com.nicefact.mailmonitor.vo.SearchBean;
import com.nicefact.mailmonitor.vo.SortInfo;



/**
 * A parser class which fetches the required data from the request object making
 * the controller less cluttered and handling parameter based value object
 * population.
 * 
 * 
 */
@Component
public class GridRequestParser {


	private static ObjectMapper mapper = new ObjectMapper();
	/**
	 * Parse http servlet request to get data criteria out of it.
	 * 
	 * @param request
	 * @return
	 */
	public GridRequest parseDataCriteria(HttpServletRequest request) {
		GridRequest gridRequest = new GridRequest();
		
		// Setting sort criteria
		

		boolean isSearch = Boolean.valueOf(request.getParameter("_search"));
		if (isSearch) {
			gridRequest.setSearch(true);
		}else{
			gridRequest.setSort(true);
		}
		gridRequest.setSearchBean(getSearchBean(request));
		// Setting pagination info
		gridRequest.setPaginationInfo(getPaginationInfo(request));
		gridRequest.setSortInfo(getSortInfo(request));
		return gridRequest;
	}
	
	private SearchBean getSearchBean(HttpServletRequest request){
		SearchBean searchBean =  new SearchBean();
		String type = request.getParameter("visitorType");
		searchBean.setType(type);
		String dateFrom = request.getParameter("dateFrom");
		searchBean.setDateFrom(dateFrom);
		String dateTo = request.getParameter("dateTo");
		searchBean.setDateTo(dateTo);
		String searchStr = request.getParameter("searchStr");
		searchBean.setSearchStr(searchStr);
		if(searchStr != null || dateFrom !=null || dateTo!=null){
			searchBean.setAdvanceSearch(true);
		}
		
		return searchBean;
		
		
	}

	/**
	 * Parse http servlet request to get sort criteria.
	 * 
	 * @param request
	 * @return
	 */
	private SortInfo getSortInfo(HttpServletRequest request) {
		SortInfo sortCriteria = new SortInfo();
		String sortOn = request.getParameter("sidx");
		sortCriteria.setSortField(StringUtils.isBlank(sortOn) ? "key" : sortOn);
		sortCriteria.setSortOrder(request.getParameter("sord"));

		return sortCriteria;
	}

	/**
	 * Parse http servlet request to get Pagination info.
	 * 
	 * @param request
	 * @return
	 */
	private PaginationInfo getPaginationInfo(HttpServletRequest request) {
		PaginationInfo info = new PaginationInfo();
		int page =0;
		if(!StringUtils.isEmpty(request.getParameter("page"))){
		page= Integer.parseInt(request.getParameter("page"));
	}
		info.setPageNumber(page > 0 ? page : 1);
		if(!StringUtils.isEmpty(request.getParameter("rows"))){
		info.setRecordsPerPage(Integer.parseInt(request.getParameter("rows")));
		}
		return info;
	}


}
